package com.zqwzd.action.json;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.zqwzd.entity.Discounts;
import com.zqwzd.service.IDiscountsService;
import com.zqwzd.util.DataExchangeModel;
import com.zqwzd.util.StringTools;

public class DiscountsJsonAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;

	private IDiscountsService discountsService;

	private Discounts discounts =new Discounts();

	/**
	 * 查看优惠详细信息
	 */
	public String id;
	/**
	 * 控制翻页
	 */
	private int start, limit;
	
	/** 业务处理返回数据 */
	public Map<String, Object> data;

	public String queryDiscounts() {
		JSONArray jsonArray	= null;
		Map<String, String> paramsM = new HashMap<String, String>();
		DataExchangeModel dataExchangeModel = new DataExchangeModel();
		try {
			paramsM.put("id", id);
			Discounts discounts = discountsService.queryDiscounts(paramsM);
			convertData(discounts);
			dataExchangeModel.setResponseState(0);
			dataExchangeModel.setResponseData(discounts);
			HttpServletResponse res = ServletActionContext.getResponse();
			res.setHeader("Access-Control-Allow-Origin", "*");
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/json");
			jsonArray	=	JSONArray.fromObject(dataExchangeModel);
			res.getWriter().write(jsonArray.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private void convertData(Discounts discounts){
		HttpServletRequest req =  ServletActionContext.getRequest();
		discounts.setImgsrc(req.getSession().getServletContext().getRealPath("/")+"images/" + discounts.getImgsrc());
		discounts.setShopImgSrc(req.getSession().getServletContext().getRealPath("/")+"images/" + discounts.getShopImgSrc());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			discounts.setBegintime(sdf.format(sdf.parse(discounts.getBegintime())));
			discounts.setEndtime(sdf.format(sdf.parse(discounts.getEndtime())));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据查询条件查询的总数
	 * @param paramsM
	 */
	private void queryDatasCount(Map<String, Object> paramsM) {
		if(data == null){
			data = new HashMap<String, Object>();
		}
		data.put("count", discountsService.queryCountByCondition(paramsM));
	}

	public String queryListDiscounts() {
		JSONArray jsonArray	= null;
		Map<String, Object> paramsM = new HashMap<String, Object>();
		DataExchangeModel dataExchangeModel = new DataExchangeModel();
		try {
			if (start <= 0) {
				start = 0;
			}
			if (limit <= 0) {
				limit = 10;
			}
			paramsM.put("start", start);
			paramsM.put("limit", limit);
			if (!StringTools.isEmpty(discounts.getType())) {
				paramsM.put("type", discounts.getType());
			}
			if (!StringTools.isEmpty(discounts.getShopName())) {
				paramsM.put("shopName", discounts.getShopName());
			} 
			List<Discounts> discountList = discountsService
					.queryListDiscounts(paramsM);
			
			for (Discounts discounts : discountList) {
				convertData(discounts);
			}
			queryDatasCount(paramsM);
			dataExchangeModel.setResponseState(0);
			dataExchangeModel.setResponseData(discountList);
			HttpServletResponse res = ServletActionContext.getResponse();
			res.setHeader("Access-Control-Allow-Origin", "*");
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/json");
			jsonArray	=	JSONArray.fromObject(dataExchangeModel);
			res.getWriter().write(jsonArray.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 更新被赞数量,指定商品ID
	 * 
	 * @return
	 */
	public String updateApproval(){
		HttpServletResponse res = ServletActionContext.getResponse();
		res.setHeader("Access-Control-Allow-Origin", "*");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/json");
		discounts.setId(id);
		discountsService.updateApproval(discounts);
		return null;
	}

	public IDiscountsService getDiscountsService() {
		return discountsService;
	}

	public void setDiscountsService(IDiscountsService discountsService) {
		this.discountsService = discountsService;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public Discounts getDiscounts() {
		return discounts;
	}

	public void setDiscounts(Discounts discounts) {
		this.discounts = discounts;
	}

}
